﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PasswordManager
{
   [Serializable]
   class KeyPairGroupList
   {
      protected List<KeyPairGroup> m_sItemList;

      public KeyPairGroupList()
      {
         m_sItemList = new List<KeyPairGroup>();
      }

      public int Count
      {
         get
         {
            return m_sItemList.Count();
         }
      }

      public void Clear()
      {
         m_sItemList.Clear();
      }

      public bool NewGroup(string strName)
      {
         bool bRet = false;
         if (m_sItemList.Exists(sItem => (sItem.Name.Equals(strName) == true)) == false)
         {
            KeyPairGroup sGrp = new KeyPairGroup();
            sGrp.Name = strName;
            m_sItemList.Add(sGrp);
            bRet = true;
         }
         return bRet;
      }

      public bool AddTo(string strName, string strKey, string strValue)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            bRet = m_sItemList[lIdx].Add(strKey, strValue);
         }
         return bRet;
      }

      public bool EditName(string strCurrName, string strNewName)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strCurrName) == true));
         if (lIdx != -1)
         {
            m_sItemList[lIdx].Name = strNewName;
            bRet = true;
         }
         return bRet;
      }

      public bool EditKey(string strName, string strCurrKey, string strNewKey)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            bRet = m_sItemList[lIdx].ModifyKey(strCurrKey, strNewKey);
         }
         return bRet;
      }

      public bool EditValue(string strName, string strKey, string strValue)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            bRet = m_sItemList[lIdx].ModifyValue(strKey, strValue);
         }
         return bRet;
      }

      public bool RemoveGroup(string strName)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            m_sItemList.RemoveAt(lIdx);
            bRet = true;
         }
         return bRet;
      }

      public bool RemoveValue(string strName, string strKey, string strValue)
      {
         bool bRet = false;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            bRet = m_sItemList[lIdx].Remove(strKey, strValue);
         }
         return bRet;
      }

      public KeyValuePair<string, string>[] ListKeyValuePairs(string strName)
      {
         KeyValuePair<string, string> []sItems = null;
         int lIdx = m_sItemList.FindIndex(sItem => (sItem.Name.Equals(strName) == true));
         if (lIdx != -1)
         {
            sItems = m_sItemList[lIdx].List();
         }
         return sItems;
      }

      public KeyPairGroup[] ListGroups()
      {
         KeyPairGroup[] sTmpLst = null;
         if (m_sItemList.Count() > 0)
         {
            sTmpLst = new KeyPairGroup[m_sItemList.Count()];
            m_sItemList.CopyTo(sTmpLst);
         }
         return sTmpLst;
      }
   }
}
